package com.sample.videocollection.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019-01-19.
 */

public abstract class BaseBindingAdapter<M> extends RecyclerView.Adapter<BindingViewHolder> {
    private Context mContext;
    private List<M> mList;

    public OnItemClickListener mOnItemClickListener;

    public BaseBindingAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }


    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(viewType), parent, false);
        return new BindingViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        this.onBindItem(holder, position);
    }

    @Override
    public int getItemCount() {
        return mList==null?0:mList.size();
    }

    public void setData(List<M> list) {
        if (list != null) {

            mList = list;
            notifyDataSetChanged();
        }else {
            mList = new ArrayList<>();
            LogUtils.e("传过来的list 对象是null");
        }
    }

    public void addData(List<M> list) {
        mList.addAll(list);
    }

    public List<M> getData() {
        return mList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    protected abstract @LayoutRes
    int getLayoutId(int viewType);

    protected abstract void onBindItem(@NonNull BindingViewHolder holder, int position);




}
